package com.lizhen.core.basicdatatype;

import java.io.*;
import java.util.Scanner;

public class IODataType {
    public static void main(String[] args) {
        char c1 = ' ';
        String s12 = "";
        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
        do {
            try {
                c1 = (char) br1.read();

                System.out.print(c1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (c1 != 'q');

        System.out.println();

        BufferedReader br12 = new BufferedReader(new InputStreamReader(System.in));
        do {
            try {
                s12 = br12.readLine();

                System.out.println(s12);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (!s12.equals("end"));
        int i13 = 'A';

        System.out.write(i13);
        System.out.write('\n');

        try {
            byte bWrite[] = {11, 21, 3, 40, 5};
            String s2 = "1.8\\api\\main\\resource\\com\\lizhen\\core\\basicdatatype\\IODataType\\binary.txt";
            OutputStream os2 = new FileOutputStream(s2);
            for (int x = 0; x < bWrite.length; x++) {
                os2.write(bWrite[x]); // writes the bytes
            }
            os2.close();
            InputStream is2 = new FileInputStream(s2);
            int size = is2.available();

            for (int i = 0; i < size; i++) {
                System.out.print((char) is2.read() + "  ");
            }
            System.out.println();

            is2.close();
        } catch (IOException e) {

            System.out.print("Exception");
        }
        String s22 = "1.8\\api\\main\\resource\\com\\lizhen\\core\\basicdatatype\\IODataType\\text.txt";
        File f22 = new File(s22);
        FileOutputStream fos22 = null;
        try {
            fos22 = new FileOutputStream(f22);
            // 构建FileOutputStream对象,文件不存在会自动新建
            OutputStreamWriter osw22 = new OutputStreamWriter(fos22, "UTF-8");
            // 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
            osw22.append("中文输入");
            // 写入到缓冲区
            osw22.append("\r\n");
            // 换行
            osw22.append("English");
            // 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入
            osw22.close();
            // 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
            fos22.close();
            // 关闭输出流,释放系统资源
            FileInputStream fis22 = new FileInputStream(f22);
            // 构建FileInputStream对象
            InputStreamReader isr22 = new InputStreamReader(fis22, "UTF-8");
            // 构建InputStreamReader对象,编码与写入相同
            StringBuffer sb22 = new StringBuffer();
            while (isr22.ready()) {
                sb22.append((char) isr22.read());
                // 转成char加到StringBuffer对象中
            }

            System.out.println(sb22.toString());

            isr22.close();
            // 关闭读取流
            fis22.close();
            // 关闭输入流,释放系统资源
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 从键盘接收数据
        // next方式接收字符串
//        Scanner scan3 = new Scanner(System.in);

//        System.out.print("next方式接收：");

//        // 判断是否还有输入
//        if (scan3.hasNext()) {
//            String s3 = scan3.next();

//            System.out.println("输入的数据为：" + s3);
//        }
//        scan3.close();
        // 从键盘接收数据
        // nextLine方式接收字符串
        Scanner scan32 = new Scanner(System.in);

        System.out.print("nextLine方式接收：");

        // 判断是否还有输入
        if (scan32.hasNextLine()) {
            String s32 = scan32.nextLine();

            System.out.println("输入的数据为：" + s32);
        }
        scan32.close();
    }
}
